﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SuperSocket.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using SuperSocket.SocketEngine.Configuration;
using System.Web.UI;
using SuperWebSocket;
using System.Configuration;

namespace TesWebSocket
{
    public class WebSocketHost
    {
        private List<WebSocketSession> m_Sessions = new List<WebSocketSession>();
        private object m_SessionSyncRoot = new object();
        private Timer m_SecureSocketPushTimer;
        private int port;

        public void Run()
        {
            LogUtil.Setup();
            startSuperWebSocket();
        }

        private void startSuperWebSocket()
        {
            var serverConfig = ConfigurationManager.GetSection("socketServer") as SocketServiceConfig;
            if (!SocketServerManager.Initialize(serverConfig))
                return;

            if (!SocketServerManager.Start())
                SocketServerManager.Stop();
        }

        //void socketServer_NewMessageReceived(WebSocketSession session, string e)
        //{
        //    SendToAll(session.Cookies["name"] + ": " + e);
        //}

        //void socketServer_SessionClosed(WebSocketSession session, CloseReason reason)
        //{
        //    lock (m_SessionSyncRoot)
        //    {
        //        m_Sessions.Remove(session);
        //    }
        //}

        //void socketServer_NewSessionConnected(WebSocketSession session)
        //{
        //    lock (m_SessionSyncRoot)
        //    {
        //        m_Sessions.Add(session);
        //    }
        //}

        //void SendToAll(string message)
        //{
        //    lock (m_SessionSyncRoot)
        //    {
        //        foreach (var s in m_Sessions)
        //        {
        //            //s.SendResponse(message);
        //            s.SendResponseAsync(message);
        //        }
        //    }
        //}
    }
}